package com.shop.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.shop.bean.Admin;
import com.shop.dao.AdminDao;
import com.shop.dao.BaseDao;

public class AdminDaoImpl implements AdminDao {

	@Override
	public Admin selectAdmin(String account, String password) {
		Admin admin=null;
		Connection con=BaseDao.getCon();
		ResultSet res=null;
		PreparedStatement ps=null;
		try {
			String sql="select * from admin where account=? and password=?";
			ps=con.prepareStatement(sql);
			ps.setString(1, account);
			ps.setString(2,password);
			res=ps.executeQuery();
			if(res.next()) {
				admin= new Admin();
				admin.setAdminId(res.getInt("adminId"));
				admin.setAccount(res.getString("account"));
				admin.setPassword(res.getString("password"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(res, ps, con);
		}
		return admin;
	}

	@Override
	public Admin selectAdminByAccount(String account) {
		Admin admin=null;
		Connection con=BaseDao.getCon();
		ResultSet res=null;
		PreparedStatement ps=null;
		try {
			String sql="select * from admin where account=?";
			ps=con.prepareStatement(sql);
			ps.setString(1, account);
			res=ps.executeQuery();
			if(res.next()) {
				admin= new Admin();
				admin.setAdminId(res.getInt("adminId"));
				admin.setAccount(res.getString("account"));
				admin.setPassword(res.getString("password"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(res, ps, con);
		}
		return admin;
	}

}
